package com.ruoyi.vstac.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.vstac.domain.Category;
import com.ruoyi.vstac.domain.EntryScore;
import com.ruoyi.vstac.service.DeadlineService;
import com.ruoyi.vstac.service.ICategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vstac.domain.Score;
import com.ruoyi.vstac.service.IScoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评分Controller
 * 
 * @author kingner
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/vstac/score")
public class ScoreController extends BaseController
{
    @Autowired
    private IScoreService scoreService;
    @Autowired
    private ICategoryService categoryService;
    @Resource
    private DeadlineService deadlineService;

    /**
     * 查询评分列表
     */
    @PreAuthorize("@ss.hasPermi('vstac:score:list')")
    @GetMapping("/list")
    public TableDataInfo list(EntryScore entryScore)
    {
        //获取用户id， 并查询评委所属组别
        Long uid = getLoginUser().getUserId();
        Category category = categoryService.selectCategoryByUserId(uid);
        if (category != null){
            entryScore.setCategoryName(category.getCategoryName());
            entryScore.setCategoryId(category.getCategoryId());
        }
        startPage();
        List<EntryScore> list = scoreService.selectEntryScoreList(entryScore);
        return getDataTable(list);
    }

    /**
     * 导出评分列表
     */
    @PreAuthorize("@ss.hasPermi('vstac:score:export')")
    @Log(title = "评分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Score score)
    {
        List<Score> list = scoreService.selectScoreList(score);
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        util.exportExcel(response, list, "评分数据");
    }

    /**
     * 获取评分详细信息
     */
    @PreAuthorize("@ss.hasPermi('vstac:score:query')")
    @GetMapping(value = "/{scoreId}")
    public AjaxResult getInfo(@PathVariable("scoreId") int scoreId)
    {
        return success(scoreService.selectScoreByScoreId(scoreId));
    }

    /**
     * 新增评分
     */
    @PreAuthorize("@ss.hasPermi('vstac:score:add')")
    @Log(title = "评分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Score score)
    {
        //判断是否过了评审时间
        try{
            deadlineService.isReviewTime();
        }catch (Exception e){
            return AjaxResult.error("Error: " + e.getMessage());
        }
        score.setUserId(getLoginUser().getUserId());
        score.setCreateBy(getLoginUser().getUser().getUserName());
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改评分
     */
    @PreAuthorize("@ss.hasPermi('vstac:score:edit')")
    @Log(title = "评分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Score score)
    {
        //判断是否过了评审时间
        try{
            deadlineService.isReviewTime();
        }catch (Exception e){
            return AjaxResult.error("Error: " + e.getMessage());
        }
        // 更新更改信息
        score.setUpdateBy(getLoginUser().getUser().getUserName());
        score.setUpdateTime(DateUtils.getNowDate());

        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除评分
     */
    @PreAuthorize("@ss.hasPermi('vstac:score:remove')")
    @Log(title = "评分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scoreIds}")
    public AjaxResult remove(@PathVariable int[] scoreIds)
    {
        return toAjax(scoreService.deleteScoreByScoreIds(scoreIds));
    }
}
